﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;

namespace FastGPT_Admin.Tool
{
    internal class AesTool
    {
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">初始化向量</param>
        /// <returns></returns>
        public static byte[] Encrypt(string plainText, byte[] key, byte[] iv)
        {
            using (Aes aes = Aes.Create()) // 创建AES对象
            {
                aes.Key = key; // 设置密钥
                aes.IV = iv; // 设置初始化向量
                aes.Mode = CipherMode.CBC; // 设置加密模式为CBC
                aes.Padding = PaddingMode.PKCS7; // 设置填充模式为PKCS7

                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); // 创建加密器

                using (MemoryStream ms = new MemoryStream()) // 创建内存流
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) // 创建加密流
                    {
                        using (StreamWriter sw = new StreamWriter(cs)) // 创建StreamWriter用于写入数据
                        {
                            sw.Write(plainText); // 写入明文
                        }
                        return ms.ToArray(); // 返回加密后的字节数组
                    }
                }
            }
        }

        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="cipherText">密文</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">初始化向量</param>
        /// <returns></returns>
        public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv)
        {
            using (Aes aes = Aes.Create()) // 创建AES对象
            {
                aes.Key = key; // 设置密钥
                aes.IV = iv; // 设置初始化向量
                aes.Mode = CipherMode.CBC; // 设置加密模式为CBC
                aes.Padding = PaddingMode.PKCS7; // 设置填充模式为PKCS7

                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); // 创建解密器

                using (MemoryStream ms = new MemoryStream(cipherText)) // 创建包含密文的内存流
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) // 创建解密流
                    {
                        using (StreamReader sr = new StreamReader(cs)) // 创建StreamReader用于读取数据
                        {
                            return sr.ReadToEnd(); // 读取解密后的字符串并返回
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 例子
        /// </summary>
        public static void MainTest()
        {
            string plainText = "这是要加密的明文!"; // 要加密的明文
            byte[] key = new byte[16]; // 128位密钥
            byte[] iv = new byte[16]; // 128位初始化向量

            // 生成一个随机的密钥和IV（演示目的）
            // 在实际应用中，应该安全地存储这些密钥和IV，并在加解密时重新使用它们
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                rng.GetBytes(key);
                rng.GetBytes(iv);
            }

            // 加密明文
            byte[] encrypted = Encrypt(plainText, key, iv);

            // 解密密文
            string decrypted = Decrypt(encrypted, key, iv);

            Console.WriteLine("明文: " + plainText);
            Console.WriteLine("加密后: " + Convert.ToBase64String(encrypted));
            Console.WriteLine("解密后: " + decrypted);
        }

        public static string EncryptString(string plainText)
        {
            byte[] key = new byte[16]; // 128位密钥
            byte[] iv = new byte[16]; // 128位初始化向量
            key = KeyGenerator.GenerateFixedKey("1234567890123456", 16);
            iv = KeyGenerator.GenerateFixedKey("1234567890123456", 16);
            // 加密密文
            byte[] encrypted = Encrypt(plainText, key, iv);

            return Convert.ToBase64String(encrypted);
        }

        public static string DecryptString(string base64Encrypted)
        {
            byte[] key = new byte[16]; // 128位密钥
            byte[] iv = new byte[16]; // 128位初始化向量
            key = KeyGenerator.GenerateFixedKey("1234567890123456", 16);
            iv = KeyGenerator.GenerateFixedKey("1234567890123456", 16);
            // 将 Base64 编码的字符串转换回字节数组
            string decrypted = Decrypt(Convert.FromBase64String(base64Encrypted), key, iv);

            return decrypted;
        }

        public static string EncryptString2(string plainText)
        {
            byte[] key = new byte[16]; // 128位密钥
            byte[] iv = new byte[16]; // 128位初始化向量
            key = KeyGenerator.GenerateFixedKey("1234567890123456", 16);
            iv = KeyGenerator.GenerateFixedKey("1234567890123456", 16);
            // 加密密文
            byte[] encrypted = Encrypt(plainText, key, iv);

            string base64Encrypted = Convert.ToBase64String(encrypted);

            // 解密Base64编码的字符串
            byte[] decryptedBytes = Convert.FromBase64String(base64Encrypted);
            string decryptedString = Decrypt(decryptedBytes, key, iv);

            return decryptedString;
        }
    }
}
